package com.ult.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.swing.text.html.parser.Entity;

import com.ult.model.Livro;
import com.ult.util.Constantes;

/**
 * classe responsavel por fazer a conexao e 
 * chamada do execute sql pela Modelo DAO
 * @author gustavo, professor
 * @category persistencia
 */
public abstract class DAO {
	
	protected boolean executar( String sql ) {
		
        Connection con = null;
        Statement stmnt = null;
        int regAfetados = 0;
        
 		try {
			Class.forName( Constantes.DRIVER);
			/** carrega o driver, username e password do bamco de dados*/
			con = DriverManager.getConnection( Constantes.URL, Constantes.USERNAME, Constantes.PASSWD);
			
			stmnt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
			
			regAfetados = stmnt.executeUpdate( sql );
			
		} catch ( SQLException se) {
			while (se != null) {
				System.out.println("State  : " + se.getSQLState());
				System.out.println("Message: " + se.getMessage());
				System.out.println("Error  : " + se.getErrorCode());

				se = se.getNextException();
			}
		} catch ( ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmnt != null ) 
						stmnt.close();
				if ( con != null ) 
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return regAfetados==1;
	}
	
	
	
	protected List<Livro> listar (String sql){
        Connection con = null;
        Statement stmnt = null;
        ResultSet rs = null;
        List<Livro> books = new ArrayList<Livro>();
        Livro book = null;
        
 		try {
 			
			Class.forName( Constantes.DRIVER);
			
			con = DriverManager.getConnection( Constantes.URL, Constantes.USERNAME, Constantes.PASSWD);
			
			stmnt = con.createStatement( );
			
			rs = stmnt.executeQuery( sql );
			
			while ( rs.next() ) {
				
				
				
				book = new Livro();
				book.setNomeLivro( rs.getString("nome_livro") );
				book.setAutor( rs.getString("tx_autor") );
				book.setGenero(rs.getString("tx_genero") );
				books.add( book );
			}
		} catch ( SQLException se) {
			while (se != null) {
				System.out.println("State  : " + se.getSQLState());
				System.out.println("Message: " + se.getMessage());
				System.out.println("Error  : " + se.getErrorCode());

				se = se.getNextException();
			}
		} catch ( ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// Fecha ResultSset, Statement\PreparedStatement e Connection
			try {
				// Fecha ResultSet
				if (rs != null)
					rs.close();
				// Fecha Statement
					if (stmnt != null)
						stmnt.close();
				// Fecha conexao
				if (con != null)
					con.close();
			} catch (SQLException SQLe) {
				// Tratar SQLException
				SQLe.printStackTrace();
			}
		}
		return books;
	}
	
	
	
}
